﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

using MissedMemo.MeatAndCheeseDisc.BusinessLogic;


namespace MissedMemo.MeatAndCheeseDisc.OrderEntry
{
    public partial class MainWindow : Window
    {
        private DispatcherTimer _timer;
        
        public MainWindow()
        {
            InitializeComponent();

            _timer = new DispatcherTimer();
            _timer.Tick += OnTimerTick;
            _timer.Start();
        }


        private void OnTimerTick( object sender, EventArgs e )
        {
            foreach( Order order in listOrders.Items )
            {
                order.UpdateProcessingTime();
            }
        }


        private void OnAddOrder( object sender, RoutedEventArgs e )
        {
            OrderEditPanel.DataContext = new Order();
            OrderEditPanel.Open( layoutRoot.ActualHeight );
        }


        private void OnEditOrder( object sender, RoutedEventArgs e )
        {
            Order order = listOrders.SelectedItem as Order;

            if( order != null )
            {
                // Binding to a COPY enables cancelling edit changes
                OrderEditPanel.DataContext = order.Clone();
                OrderEditPanel.Open( layoutRoot.ActualHeight );
            }
        }


        private void OnDeleteOrder( object sender, RoutedEventArgs e )
        {
            Order order = listOrders.SelectedItem as Order;

            if( order != null )
            {
                int i = App.OrderCollection.IndexOf( order );
                App.OrderCollection.Remove( order );

                listOrders.SelectedIndex = (listOrders.Items.Count > i)? i : listOrders.Items.Count - 1;
            }
        }


        // Note: We could update App.OrderCollection directly in the edit view,
        // but it feels "correct" to group it here with Add/Edit/Delete opps,
        // and also eventually coordinate with a remote comms helper class here.

        private void OnRequestSaveEditChanges( object sender, ExecutedRoutedEventArgs e )
        {
            Order editedOrder = OrderEditPanel.DataContext as Order;

            if( editedOrder != null )
            {
                Order originalOrder = App.OrderCollection.FirstOrDefault( order => order.NowServingNumber == editedOrder.NowServingNumber );

                if( originalOrder == null ) // add NEW order
                {
                    editedOrder.DateTimeOrderPlaced = DateTime.Now;
                    App.OrderCollection.Add( editedOrder );
                }
                else // update EXISTING order
                {
                    int i = App.OrderCollection.IndexOf( originalOrder );
                    App.OrderCollection[i] = editedOrder;
                }

                listOrders.SelectedItem = editedOrder;
            }
        }
    }
}
